<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/10
 * Time: 12:01
 */

namespace plugin\admin\app\logic\plan;


use app\common\lib\Constant;
use app\exception\IndexException;
use plugin\admin\app\logic\BaseLogic;
use plugin\admin\app\model\font\FontModel;
use plugin\admin\app\model\image\ImageModelModel;
use plugin\admin\app\model\material\MaterialDataModel;
use plugin\admin\app\model\novel\NovelChapterModel;
use plugin\admin\app\model\novel\NovelChapterPartModel;
use plugin\admin\app\model\novel\NovelSpreadModel;
use plugin\admin\app\model\plan\BackgroundMusicModel;
use plugin\admin\app\model\moyin\TimbreModel;
use plugin\admin\app\model\plan\PromotionPlanOverseaModel;
use support\Request;
use support\Response;

class OverseaLogic extends BaseLogic
{
    protected static $instance;

    public function __construct()
    {
        $this->model = new PromotionPlanOverseaModel();
    }

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function select(Request $request): Response
    {
        $field = $request->input('field');
        if($field==false){
            $request->inputSet('field','created_at');
            $request->inputSet('order','desc');
        }

        $request->inputSet('isdel','0');
        return parent::select($request);
    }

    protected function formatNormal($items, $total): Response
    {
//        $novelMap = $this->getListMapById(NovelSpreadModel::getInstance(),$items,'novel_sn','novel_sn');
//        $chapterMap = $this->getListMapById(NovelChapterModel::getInstance(),$items,'chapterid','id');
        $musicMap = $this->getListMapById(BackgroundMusicModel::getInstance(),$items,'musicid','id');
        $timbreMap = $this->getListMapById(TimbreModel::getInstance(),$items,'timbreid','id');
        $fontMap = $this->getListMapById(FontModel::getInstance(),$items,'fontid','id');
        $langMap = [
            'en'=>'英文',
            'id'=>'印尼语',
            'th'=>'泰语',
        ];
        foreach($items as &$val){
//            $val['title'] = isset($novelMap[$val['novel_sn']])?$novelMap[$val['novel_sn']]['title']:$val['novel_sn'];
//            $val['content'] = isset($chapterMap[$val['chapterid']])?$chapterMap[$val['chapterid']]['content']:$val['chapterid'];
//            $val['lang'] = isset($chapterMap[$val['chapterid']])?$chapterMap[$val['chapterid']]['lang']:'';

            $val['musicname'] = isset($musicMap[$val['musicid']])?$musicMap[$val['musicid']]['name']:$val['musicid'];
            $val['music_url'] = isset($musicMap[$val['musicid']])?$musicMap[$val['musicid']]['music_url']:'';

            $val['fontname'] = isset($fontMap[$val['fontid']])?$fontMap[$val['fontid']]['name']:$val['fontid'];

            $val['timbrename'] = isset($timbreMap[$val['timbreid']])?
                                 $timbreMap[$val['timbreid']]['name'].'('.$timbreMap[$val['timbreid']]['lang'].')'.'('.$timbreMap[$val['timbreid']]['scenes'].')':$val['timbreid'];

            //素材
            $materialNames = MaterialDataModel::getInstance()->getWhere([['id','in',explode(',',$val['material_id'])],'isdel'=>0])->pluck('name')->toArray();
            $val['material_name'] = implode(',',$materialNames);

            $val['settings'] = $val['settings'] ? json_decode($val['settings'], true) : [];

            $val['lang_text'] = $langMap[$val['lang']]??'';
        }
        return parent::formatNormal($items, $total);
    }

    public function insert(Request $request): Response
    {
        $settings = $request->input('settings');
        $settingsKeys = ['random_reduce','random_add','random_speed','random_mirror','random_hash','random_filter','random_effect','random_transition','random_enlarge'];
        foreach ($settingsKeys as $val){
            $settings[$val] = isset($settings[$val]) && $settings[$val]=='on' ? 1:0;
        }
        //$request->inputSet('status',0);
        $request->inputSet('settings',json_encode($settings,256));

        return parent::insert($request);
    }

    protected function doInsert(array $data)
    {
        if(!empty($data['lang'])){
//            $data['novel_sn'] = NovelChapterModel::getInstance()->getWhere(['id'=>$data['chapterid']])->value('novel_sn');
//            $data['spread_type'] = NovelSpreadModel::getInstance()->getWhere(['novel_sn'=>$data['novel_sn']])->value('spread_type');
//            $data['lang'] = NovelSpreadModel::getInstance()->getWhere(['novel_sn'=>$data['novel_sn']])->value('lang');

            $lang = $data['lang'];
            $tlang = TimbreModel::getInstance()->getWhere(['id'=>$data['timbreid']])->value('lang');
            $lang2 = Constant::LANGUAGEMAP[$lang]??'普通话';
            if($lang2!=$tlang){
                throw new IndexException(4020,'音色语言和小说语言不一致');
            }

            if(!empty($data['musicid'])){
                $blang = BackgroundMusicModel::getInstance()->getWhere(['id'=>$data['musicid']])->value('lang');
                $lang3 = Constant::LANGUAGEMAP[$lang]??'中文';
                if($lang3!=$blang){
                    throw new IndexException(4020,'背景音乐语言和小说语言不一致');
                }
            }
        }

        $id = parent::doInsert($data);

        return $id;
    }

    public function update(Request $request): Response
    {
        if(!empty($request->input('lang')) && !empty($request->input('timbreid'))){
            $settings = $request->input('settings');
            $settingsKeys = ['random_reduce','random_add','random_speed','random_mirror','random_hash','random_filter','random_effect','random_transition','random_enlarge'];
            foreach ($settingsKeys as $val){
                $settings[$val] = isset($settings[$val]) && $settings[$val]=='on' ? 1:0;
            }
            $request->inputSet('settings',json_encode($settings,256));
        }
        return parent::update($request);
    }

    protected function doUpdate($id, $data)
    {
        if(!empty($data['lang'])){
//            $data['novel_sn'] = NovelChapterModel::getInstance()->getWhere(['id'=>$data['chapterid']])->value('novel_sn');
//            $data['spread_type'] = NovelSpreadModel::getInstance()->getWhere(['novel_sn'=>$data['novel_sn']])->value('spread_type');

            $lang = $data['lang'];
            $tlang = TimbreModel::getInstance()->getWhere(['id'=>$data['timbreid']])->value('lang');
            $lang2 = Constant::LANGUAGEMAP[$lang]??'普通话';
            if($lang2!=$tlang){
                throw new IndexException(4020,'音色语言和小说语言不一致');
            }
            if(!empty($data['musicid'])){
                $blang = BackgroundMusicModel::getInstance()->getWhere(['id'=>$data['musicid']])->value('lang');
                $lang3 = Constant::LANGUAGEMAP[$lang]??'中文';
                if($lang3!=$blang){
                    throw new IndexException(4020,'背景音乐语言和小说语言不一致');
                }
            }
        }

        return parent::doUpdate($id, $data);
    }

    public function delete(Request $request): Response
    {
        return parent::isDelete($request);
    }

    public function doIsDelete($ids)
    {
        parent::doIsDelete($ids);

        foreach ($ids as $id){
            $novelSn = $this->model->where(['id'=>$id])->value('novel_sn');
            $planNums = $this->model->getWhere(['id'=>$id,'isdel'=>0])->count();
            NovelSpreadModel::getInstance()->getWhere(['novel_sn'=>$novelSn])->update(['plan_nums'=>$planNums]);
        }
    }
}